package com.gjy.mongod.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-15 10:31:38
 */
@Slf4j
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        String dateString = parser.getText();
        LocalDateTime localDateTime = LocalDateTime.parse(dateString, formatter)
                .atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
        log.info("local datetime: {}", localDateTime);
        return localDateTime;
    }

}
